
/**
 * Dropdown
 */

layui.define([ 'jquery' ], function(exports) {

  var MOD_NAME = 'dropdown', CLASS_NAME = '.layui-ext-dropdown', $ = layui.jquery;

  var dropdown = {
    "v" : '1.0.0'
  };

  // 渲染
  dropdown.render = function(opt) {
    var settings = {
      event : 'click'
    };
    settings = $.extend({}, settings, opt);

    $(CLASS_NAME).each(
        function(i, elem) {
          var jqelem = $(elem);
          var ulBox = jqelem.children('ul');
          var timer = '';
          ulBox.addClass('layui-anim layui-anim-upbit');

          var triggerEl = jqelem;
          var event = settings.event || 'mouseenter';
          if (jqelem.hasClass('dropdown-click'))
            event = 'click';
          if (jqelem.hasClass('dropdown-mouseenter'))
            event = 'mouseenter';
          if (jqelem.hasClass('dropdown-click-btn')) {
            triggerEl = $('.button-dropdown', elem);
            event = 'click';
          }

          triggerEl.on(event, function(e) {
            var disabled = jqelem.hasClass('dropdown-disabled');
            if (disabled) {
              return false;
            }
            clearTimeout(timer);
            // FIX 定位
            var overHeightBottom = (jqelem.offset().top + jqelem.height() + ulBox.outerHeight() - $(window).scrollTop()) > $(window).height();
            var overHeightTop = (jqelem.offset().top - $(window).scrollTop() - ulBox.outerHeight()) < 0;
            var heightToBottom = $(window).height() - jqelem.offset().top - jqelem.height() - $(window).scrollTop() - 50;
            var overWidth = (jqelem.offset().left + ulBox.outerWidth() - $(window).scrollLeft()) > $(window).width() && (jqelem.offset().left > ulBox.outerWidth());
            if (overHeightBottom) {
              ulBox.css({
                "top" : "auto",
                "bottom" : "100%"
              });
            } else {
              ulBox.css({
                "top" : "100%",
                "bottom" : "auto"
              });
            }
            if (overWidth) {
              ulBox.css({
                "left" : "auto",
                "right" : "0"
              });
            } else {
              ulBox.css({
                "left" : "0",
                "right" : "auto"
              });
            }
            if(overHeightBottom && overHeightTop) {
              ulBox.css({
                "top" : "100%",
                "bottom" : "auto",
                "height" : heightToBottom
              });
            } else {
              ulBox.css({
                "height" : 'auto'
              });
            }
            
            ulBox.show();
          });
          if (event == 'mouseenter') {
            jqelem.on("mouseleave", function(e) {
              timer = setTimeout(function() {
                ulBox.hide();
              }, 300);
            })
          } else {
            $(document).on(
                "mousedown click",
                function(e) {
                  var menu = jqelem;
                  var target = $(e.target);
                  var isSourceOrChild = menu.is(target) || target.hasClass('dropdown-target') || target.parents('.dropdown-target').length > 0
                  if (!isSourceOrChild && menu.has(target).length === 0) {
                    ulBox.hide()
                  }
                });
          }
        })
  }

  // 自动完成渲染
  dropdown.render();

  // 输出接口
  exports(MOD_NAME, dropdown);
});